import { breadth } from "treeverse";
const get = (m: [number, number][][], x: number, y: number) =>
    m?.[y]?.[x] ?? undefined;

/** 获取填充位置的函数 */
export function getColorFlood(m: [number, number][][], x: number, y: number) {
    const walkSet = new Set<string>();
    const collection: number[][] = [];
    const sample = get(m, x, y)?.join() || "";
    if (sample === "") return [];
    // console.log(sample);
    breadth({
        tree: [x, y],
        visit([x, y]: number[]) {
            collection.push([x, y]);
        },
        getChildren([x, y]: number[]) {
            return [
                [x, y + 1],
                [x, y - 1],
                [x + 1, y],
                [x - 1, y],
            ];
        },
        filter([x, y]: number[]) {
            const Sample = get(m, x, y)!;
            const tag = `${x},${y}`;
            // 如果该点的透明度为 0 ，那么直接填充
            if (
                Sample &&
                (Sample[1] <= 0 || Sample.join() === sample) &&
                !walkSet.has(tag)
            ) {
                walkSet.add(tag);
                return true;
            }
            return false;
        },
    });
    return collection;
}
